package com.qingcloud.adminbackend.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingcloud.adminbackend.constant.DictConstant;
import com.qingcloud.adminbackend.exception.WmCommonException;
import com.qingcloud.adminbackend.entity.config.SysDictionaryConfig;
import com.qingcloud.adminbackend.entity.product.SysProductTopic;
import com.qingcloud.adminbackend.entity.product.SysProductType;
import com.qingcloud.adminbackend.enums.BizExceptionEnum;
import com.qingcloud.adminbackend.mapper.SysProductTopicMapper;
import com.qingcloud.adminbackend.mapper.SysProductTypeMapper;
import com.qingcloud.adminbackend.pojo.dto.sysproduct.AddSysProductTopicDTO;
import com.qingcloud.adminbackend.pojo.dto.sysproduct.PageSysProductTopicListDTO;
import com.qingcloud.adminbackend.pojo.dto.sysproduct.UpdateSysProductTopicDTO;
import com.qingcloud.adminbackend.pojo.vo.sysproduct.PageSysProductTopicListVO;
import com.qingcloud.adminbackend.pojo.vo.sysproduct.SysProductTopicDetailVO;
import com.qingcloud.adminbackend.service.ISysDictionaryConfigService;
import com.qingcloud.adminbackend.service.ISysProductTopicService;
import com.qingcloud.adminbackend.utils.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品主题服务实现类
 * </p>
 * @author yangfan
 * @since 2021-11-09
 */
@Service
public class SysProductTopicServiceImpl extends ServiceImpl<SysProductTopicMapper, SysProductTopic> implements ISysProductTopicService {
    @Resource
    private SysProductTypeMapper sysProductTypeMapper;

    @Resource
    private ISysDictionaryConfigService iSysDictionaryConfigService;

    /**
     * 分页产品主题列表查询请求DTO
     *
     * @param pageParam 分页产品主题列表查询请求DTO
     * @return 分页产品主题列表查询返回VO
     */
    @Override
    public IPage<PageSysProductTopicListVO> pageSysProductTopicList(PageSysProductTopicListDTO pageParam) {
        final IPage<PageSysProductTopicListVO> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        getBaseMapper().pageSysProductTopicList(page, pageParam);
        List<PageSysProductTopicListVO> records = page.getRecords();
        buildPageResultVo(records);
        return page;
    }

    /**
     * 构建返回参数
     * @param records 数据库查询的返回记录
     */
    private void buildPageResultVo(List<PageSysProductTopicListVO> records) {
        if (CollectionUtil.isNotEmpty(records)) {
            Map<String, SysDictionaryConfig> topicTypeMap = iSysDictionaryConfigService.getDictMapByType(DictConstant.TOPIC_TYPE);
            Map<String, SysDictionaryConfig> platformMessageModeMap = iSysDictionaryConfigService.getDictMapByType(DictConstant.PLATFORM_MESSAGE_MODE);
            for (PageSysProductTopicListVO vo : records) {
                //主题类型
                String topicType = vo.getTopicType();
                if (StrUtil.isNotBlank(topicType)) {
                    String dictStr = DataUtil.getDictByStr(topicTypeMap, topicType);
                    vo.setTopicType_d(dictStr);
                }
                //平台消息模式
                String platformMessageMode = vo.getPlatformMessageMode();
                if (StrUtil.isNotBlank(platformMessageMode)) {
                    String dictStr = DataUtil.getDictByStr(platformMessageModeMap, platformMessageMode);
                    vo.setPlatformMessageMode_d(dictStr);
                }
            }
        }
    }

    /**
     * 查询产品主题详情
     *
     * @param id 产品主题id
     * @return 产品主题id
     */
    @Override
    public SysProductTopicDetailVO selectSysProductTopicDetailById(Integer id) {
        SysProductTopicDetailVO vo = getBaseMapper().selectSysProductTopicDetailById(id);
        if(vo!=null){
            //主题类型
            String topicType = vo.getTopicType();
            if (StrUtil.isNotBlank(topicType)) {
                SysDictionaryConfig dictConfig = iSysDictionaryConfigService.getDictByTypeAndCode(DictConstant.TOPIC_TYPE,topicType);
                vo.setTopicType_d(dictConfig.getName());
            }
            //平台消息模式
            String platformMessageMode = vo.getPlatformMessageMode();
            if (StrUtil.isNotBlank(platformMessageMode)) {
                SysDictionaryConfig dictConfig = iSysDictionaryConfigService.getDictByTypeAndCode(DictConstant.PLATFORM_MESSAGE_MODE,platformMessageMode);
                vo.setPlatformMessageMode_d(dictConfig.getName());
            }
        }
        return vo;
    }
    /**
     * 新增产品主题
     * @param addSysProductTopicDTO 新增产品主题请求DTO
     * @param currentUserId 当前用户
     * @return 产品主题id
     */
    @Override
    public Integer addSysProductTopic(AddSysProductTopicDTO addSysProductTopicDTO, String currentUserId) {
        Integer productTypeId = addSysProductTopicDTO.getProductTypeId();
        SysProductType sysProductType = sysProductTypeMapper.selectById(productTypeId);
        if (sysProductType == null) {
            throw new WmCommonException(BizExceptionEnum.PARA_ERROR, "产品信息不存在");
        }
        SysProductTopic entity = new SysProductTopic();
        cn.hutool.core.bean.BeanUtil.copyProperties(addSysProductTopicDTO, entity);
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateDt(now);
        entity.setModifyDt(now);
        entity.setCreateBy(currentUserId);
        entity.setModifyBy(currentUserId);
        save(entity);
        return entity.getId();
    }

    /**
     * 更新产品主题
     * @param updateSysProductTopicDTO 更新产品主题请求DTO
     * @param currentUserId 当前用户
     * @return 更新结果
     */
    @Override
    public Boolean updateSysProductTopicById(UpdateSysProductTopicDTO updateSysProductTopicDTO, String currentUserId) {
        SysProductTopic queryTopic = getById(updateSysProductTopicDTO.getId());
        if(queryTopic==null){
            throw new WmCommonException(BizExceptionEnum.DATA_NULL,"主题信息不存在");
        }
        SysProductTopic entity = new SysProductTopic();
        cn.hutool.core.bean.BeanUtil.copyProperties(updateSysProductTopicDTO, entity);
        LocalDateTime now = LocalDateTime.now();
        entity.setModifyDt(now);
        entity.setModifyBy(currentUserId);
        return updateById(entity);
    }

    /**
     * 删除产品主题
     *
     * @param idList 产品主题Id列表
     * @return 删除结果
     */
    @Override
    public Boolean deleteSysProductTopicList(List<Integer> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            throw new WmCommonException(BizExceptionEnum.PARA_ERROR, "产品主题列表不能为空");
        }
        return removeByIds(idList);
    }
}
